//package com.open.launcher.view.utils;
//
//import android.content.Context;
//import android.content.SharedPreferences;
//import android.util.Log;
//import android.widget.Toast;
//
//
//import com.google.gson.Gson;
//import com.google.gson.reflect.TypeToken;
//import com.open.launcher.bean.LatelyBean;
//import com.open.launcher.bean.allchannels.Program;
//import com.open.launcher.bean.details.PlayDetailsBean;
//import com.open.launcher.bean.greendao.ProgramDB;
//import com.open.launcher.bean.greendao.SitcomListGen;
//import com.open.launcher.utils.SystemTimeDate;
////import com.open.launcher.utils.greendao.DbService;
//
//import java.util.ArrayList;
//import java.util.List;
//
//
///**
// * Created by Shinelon on 2017/10/29.
// */
//
//public class CacheDataPresenter {
//    private List<LatelyBean> detailsBeanList;
//    private Gson gson = new Gson();
//    private static Context mContext;
//    private static SharedPreferences sp;
//    private static CacheDataPresenter presenter;
//
//    public static CacheDataPresenter getPresenter(Context content) {
//        mContext = content;
//        sp = mContext.getSharedPreferences("CACHE_RECORD", Context.MODE_PRIVATE);
//        if (presenter == null) {
//            presenter = new CacheDataPresenter();
//        }
//        return presenter;
//    }
//
//    /**
//     * 缓存播放记录默认20条
//     *
//     * @param detailsBean
//     */
//    public void cacheRecord(PlayDetailsBean detailsBean, String cacheName, int countNumber) {
//        String usedCache = sp.getString(cacheName, "");
//        int id = detailsBean.getId();
//        int index = 100;
//        if ("".equals(usedCache)) {
//            detailsBeanList = new ArrayList<>();
//        } else {
//            detailsBeanList = gson.fromJson(usedCache, new TypeToken<List<LatelyBean>>() {
//            }.getType());
//            int count = detailsBeanList.size();
//            if (count >= 20) {
//                detailsBeanList = detailsBeanList.subList(count - countNumber, detailsBeanList.size());
//            }
//
//            for (int i = 0; i < detailsBeanList.size(); i++) {
//                if (id == detailsBeanList.get(i).getId()) {
//                    index = i;
//                }
//            }
//            if (index < 100) {
//                detailsBeanList.remove(index);
//            }
//        }
//        LatelyBean latelyBean = new LatelyBean();
//        latelyBean.setId(detailsBean.getId());
//        latelyBean.setName(detailsBean.getName());
//        latelyBean.setCopyright(detailsBean.getCopyright());
//        latelyBean.setDirector(detailsBean.getDirector());
//        latelyBean.setInfo(detailsBean.getInfo());
//        latelyBean.setPublish_at(detailsBean.getPublish_at());
//        latelyBean.setSort(detailsBean.getSort());
//        latelyBean.setStaring(detailsBean.getStaring());
//        latelyBean.setTotal_episode(detailsBean.getTotal_episode());
//        latelyBean.setUrl(detailsBean.getUrl());
//        latelyBean.setImage(detailsBean.getImage());
//        latelyBean.setType(detailsBean.getType());
//        String time = SystemTimeDate.stampToSystemDate(System.currentTimeMillis());
//        boolean isTime = true;
//        for (int i = 0; i < detailsBeanList.size(); i++) {
//            LatelyBean lately = detailsBeanList.get(i);
//            if (time != null && time.equals(lately.getLateTime())) {
//                isTime = false;
//            }
//        }
//        if (isTime) {
//            latelyBean.setLateTime(time);
//        } else {
//            latelyBean.setLateTime("");
//        }
//        detailsBeanList.add(latelyBean);
//
//        String recordJson = gson.toJson(detailsBeanList);
//        SharedPreferences.Editor editor = sp.edit();
//        editor.putString(cacheName, recordJson);
//        editor.apply();
//    }
//
//
//    public void cacheSitcom(int id, int index) {
//        SitcomListGen sitcomListGen = null;
//        List<SitcomListGen> sitcomListGens = DbService.getInstance().loadAllNote();
//        if (sitcomListGens != null) {
//            for (int i = 0; i < sitcomListGens.size(); i++) {
//                if (id == sitcomListGens.get(i).getSitcomId()) {
//                    sitcomListGen = sitcomListGens.get(i);
//                }
//            }
//            if (sitcomListGen != null) {
//                sitcomListGen.setIndexSitcom(sitcomListGen.getIndexSitcom() + "," + index);
//                DbService.getInstance().saveNote(sitcomListGen);
//            } else {
//                sitcomListGen = new SitcomListGen(null, id, index + "");
//                DbService.getInstance().saveNote(sitcomListGen);
//            }
//        }
//    }
//
//    public String[] getSitcomIndex(int id) {
//        SitcomListGen sitcomListGen = DbService.getInstance().loadData(id);
//        if (sitcomListGen != null) {
//            String sitcomList = sitcomListGen.getIndexSitcom();
//            if (sitcomList != null) {
//                String[] sitcomArr = sitcomList.split(",");
//                return sitcomArr;
//            }
//        }
//        return null;
//    }
//
//    public void saveLiveProgram(Program program) {
//        if (program != null) {
//            String programStr = gson.toJson(program);
//            ProgramDB programDB = new ProgramDB();
//            programDB.setId((long) program.getId());
//            programDB.setProgramContent(programStr);
//            Long count = DbService.getInstance().saveProgramNote(programDB);
//            if (count > 0) {
//                Toast.makeText(mContext, "收藏成功！", Toast.LENGTH_SHORT).show();
//            } else {
//                Toast.makeText(mContext, "收藏失败！", Toast.LENGTH_SHORT).show();
//            }
//            List<ProgramDB> programDBList = DbService.getInstance().loadProgramAllNote();
//            for (ProgramDB programs : programDBList) {
//                Log.d("tag", "--------programs--------" + programs.toString());
//            }
//        }
//    }
//
//    public List<Program> loadProgramAll() {
//        List<ProgramDB> programDBList = DbService.getInstance().loadProgramAllNote();
//        List<Program> programList = new ArrayList<>();
//        for (ProgramDB programDB : programDBList) {
//            if (programDB != null) {
//                String programStr = programDB.getProgramContent();
//                if (programStr != null && !programStr.isEmpty()) {
//                    Program program = gson.fromJson(programStr, Program.class);
//                    programList.add(program);
//                }
//            }
//
//        }
//        return programList;
//    }
//}
